1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.collect.testing.MapInterfaceTest;
21  import com.google.common.collect.testing.SortedMapInterfaceTest;
22  
23  import java.util.SortedMap;
24  import java.util.TreeMap;
25  
26  /**
27   * Tests for {@link ForwardingSortedMap} using {@link MapInterfaceTest}.
28   *
29   * @author George van den Driessche
30   */
31  @GwtCompatible
32  public class ForwardingSortedMapImplementsMapTest
33      extends SortedMapInterfaceTest<String, Integer> {
34  
35    private static class SimpleForwardingSortedMap<K, V>
36        extends ForwardingSortedMap<K, V> {
37      final SortedMap<K, V> delegate;
38      SimpleForwardingSortedMap(SortedMap<K, V> delegate) {
39        this.delegate = delegate;
40      }
41      @Override protected SortedMap<K, V> delegate() {
42        return delegate;
43      }
44    }
45  
46    public ForwardingSortedMapImplementsMapTest() {
47      super(true, true, true, true, true);
48    }
49  
50    @Override protected SortedMap<String, Integer> makeEmptyMap() {
51      return new SimpleForwardingSortedMap<String, Integer>(
52          new TreeMap<String, Integer>(Ordering.natural().nullsFirst()));
53    }
54  
55    @Override protected SortedMap<String, Integer> makePopulatedMap() {
56      final SortedMap<String, Integer> sortedMap = makeEmptyMap();
57      sortedMap.put("one", 1);
58      sortedMap.put("two", 2);
59      sortedMap.put("three", 3);
60      return sortedMap;
61    }
62  
63    @Override protected String getKeyNotInPopulatedMap()
64        throws UnsupportedOperationException {
65      return "minus one";
66    }
67  
68    @Override protected Integer getValueNotInPopulatedMap()
69        throws UnsupportedOperationException {
70      return -1;
71    }
72  
73    @Override public void testContainsKey() {
74      try {
75        super.testContainsKey();
76      } catch (ClassCastException tolerated) {
77      }
78    }
79  
80    @Override public void testEntrySetContainsEntryIncompatibleKey() {
81      try {
82        super.testEntrySetContainsEntryIncompatibleKey();
83      } catch (ClassCastException tolerated) {
84      }
85    }
86    
87    @Override public void testEntrySetRemoveAllNullFromEmpty() {
88      try {
89        super.testEntrySetRemoveAllNullFromEmpty();
90      } catch (RuntimeException tolerated) {
91        // GWT's TreeMap.entrySet().removeAll(null) doesn't throws NPE.
92      }
93    }
94  
95    @Override public void testEntrySetRetainAllNullFromEmpty() {
96      try {
97        super.testEntrySetRetainAllNullFromEmpty();
98      } catch (RuntimeException tolerated) {
99        // GWT's TreeMap.entrySet().retainAll(null) doesn't throws NPE.
100     }
101   }
102 
103   @Override public void testKeySetRemoveAllNullFromEmpty() {
104     try {
105       super.testKeySetRemoveAllNullFromEmpty();
106     } catch (RuntimeException tolerated) {
107       // GWT's TreeMap.keySet().removeAll(null) doesn't throws NPE.
108     }
109   }
110 
111   @Override public void testKeySetRetainAllNullFromEmpty() {
112     try {
113       super.testKeySetRetainAllNullFromEmpty();
114     } catch (RuntimeException tolerated) {
115       // GWT's TreeMap.keySet().retainAll(null) doesn't throws NPE.
116     }
117   }
118 
119   @Override public void testValuesRemoveAllNullFromEmpty() {
120     try {
121       super.testValuesRemoveAllNullFromEmpty();
122     } catch (RuntimeException tolerated) {
123       // GWT's TreeMap.values().removeAll(null) doesn't throws NPE.
124     }
125   }
126 
127   @Override public void testValuesRetainAllNullFromEmpty() {
128     try {
129       super.testValuesRemoveAllNullFromEmpty();
130     } catch (RuntimeException tolerated) {
131       // GWT's TreeMap.values().retainAll(null) doesn't throws NPE.
132     }
133   }
134 }